﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using RSSReader.MobileClient.Framework;
using RSSReader.MobileClient.Model;

namespace RSSReader.MobileClient.ViewModel
{
    class CreateUserViewModel
    {
        public WCFWebRoleService.DataDistributorClient CurrentFeeds = new WCFWebRoleService.DataDistributorClient();
        public LUser Usr;
        public AsyncDataManager Asdm;

        public ICommand CreateUserCommand { get; private set; }

        public void AddUser(string username, string password, string email, AsyncDataManager a)
        {
            Asdm = a;
            Usr = new LUser(username, password, email);
            CurrentFeeds.CreateUserCompleted += new EventHandler<WCFWebRoleService.CreateUserCompletedEventArgs>(CallBackUserCreation);
            CurrentFeeds.CreateUserAsync(Usr.UserName, Usr.Password, Usr.Email);
        }

        private void CallBackUserCreation(object sender, WCFWebRoleService.CreateUserCompletedEventArgs e)
        {
            if (e.Result.Id != null)
                Usr.UserId = e.Result.Id;
            else
                Usr.UserId = null;
            Asdm.ManageCallbacks("UserReached");
        }
    }
}
